package com.code.demo.ui.gallery;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.code.demo.R;
import com.code.demo.ui.base.BaseActivity;
import com.code.demo.ui.common.ToastFactory;
import com.tmall.ultraviewpager.UltraViewPager;

import java.util.ArrayList;
import java.util.List;

import static com.code.demo.config.Colors.IndicatorNormal;
import static com.code.demo.config.Colors.IndicatorSelect;

/**
 * Created by yangliguo on 2018/3/11.
 */

public class GalleryActivity extends BaseActivity {

    private List<String> mDataList = new ArrayList<>();

    private UltraViewPager vp;
    private GalleryPagerAdapter adapter;
    boolean showNumber;
    private TextView tvIndex;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    finish();
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        tvIndex = (TextView) findViewById(R.id.tvIndex);

        mDataList = getIntent().getStringArrayListExtra("data");
        int select = getIntent().getIntExtra("select", 0);
        showNumber = getIntent().getBooleanExtra("showNumber", false);
        if (mDataList.size() > 10) {
            showNumber = true;
        }
        if (mDataList != null && mDataList.size() > 0) {

            vp = (UltraViewPager) findViewById(R.id.ultra_view_pager);
            vp.setScrollMode(UltraViewPager.ScrollMode.HORIZONTAL);
            adapter = new GalleryPagerAdapter(this, false);
            adapter.setData(mHandler, mDataList);
            vp.setAdapter(adapter);
            vp.setCurrentItem(select);

            if (showNumber) {
                tvIndex.setVisibility(View.VISIBLE);
                tvIndex.setText(String.valueOf(select + 1) + '/' + String.valueOf(mDataList.size()));
            } else {
                tvIndex.setVisibility(View.GONE);
                vp.initIndicator();
                vp.getIndicator().setOrientation(UltraViewPager.Orientation.HORIZONTAL)
                        .setFocusColor(IndicatorSelect)
                        .setNormalColor(IndicatorNormal)
                        .setMargin(0, 0, 0, 50)
                        .setRadius(15);
                vp.getIndicator().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
                vp.getIndicator().build();
            }

            vp.setInfiniteLoop(false);

            vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if (showNumber) {
                        tvIndex.setVisibility(View.VISIBLE);
                        tvIndex.setText(String.valueOf(position + 1) + '/' + String.valueOf(mDataList.size()));
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } else {
            ToastFactory.showToast(this, "数据错误");
            finish();
            return;
        }
    }
}